package com.kt.mes.mqtt;

import com.alibaba.fastjson.JSON;
import com.kt.mes.domain.ToMes;
import com.kt.mes.opc.OpcInteface;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Handler;

public class PushCallback implements MqttCallback {
    @Override
    public void connectionLost(Throwable throwable) {

    }

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        //System.out.println(s+":"+mqttMessage.toString());
        Map<String,Object> msg= (Map<String, Object>) JSON.parse(mqttMessage.toString());

        switch (s){

            case "from/station/function":
                System.out.println("功能消息:"+msg);//功能消息:{"data":"\"OPC\".\"To_MES\".\"fun_site\"","station":"gw01","value":{"fun_site":"10101","opos_id":"0","mes_mode":"true","o_id":"0","op_id":"0","step_num":"0","tray_id":"4"}}

                MqttInterface mqttInterface=    SpringUtils.getBean("MqttInterface");
                Map<String,Object> valueMap= (Map<String, Object>) msg.get("value");
                String fun_site=valueMap.get("funSite").toString();
                ToMes toMes=new ToMes();
                toMes.setFunSite(fun_site);
                toMes.setOposId(valueMap.get("oposId").toString());
                toMes.setoId(valueMap.get("oId").toString());
                toMes.setOpId(valueMap.get("opId").toString());
                toMes.setStepNum(valueMap.get("stepNum").toString());
                toMes.setTrayId(valueMap.get("trayId").toString());

                if(valueMap.containsKey("areaNum")){
                    toMes.setAreaNum(Integer.valueOf(valueMap.get("areaNum").toString()));
                }


                if (fun_site.length()==5) {
                    mqttInterface.funOperate(fun_site,toMes);
                }

                break;

            case "from/station/state/gw01":
               // System.out.println("gw01状态:"+msg);
                break;
        }




    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

    }
}
